
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
	/// <summary>
	/// Sys_Config Data Class
	/// </summary>
	public partial class Sys_ConfigDAL : BaseDAL
	{
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
		public string TableName = "Sys_Config";

		public Sys_ConfigDAL()
		{}

		#region  成员方法
		/// <summary>
		///  新增一条数据
		/// </summary>
		public bool Add(Sys_Config model)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("insert into [Sys_Config](");
			strSql.Append("[ConfigKey],[ConfigValue],[ConfigType],[Status],[Memo],[Creator],[CreateTime],[IsModify]");
			strSql.Append(") values (");
			strSql.Append("@ConfigKey,@ConfigValue,@ConfigType,@Status,@Memo,@Creator,@CreateTime,@IsModify");
			strSql.Append(")");

			SqlParameter[] parameters = {
				  
					new SqlParameter("@ConfigKey", model.ConfigKey),
					new SqlParameter("@ConfigValue", model.ConfigValue),
					new SqlParameter("@ConfigType", model.ConfigType),
					new SqlParameter("@Status", model.Status),
					new SqlParameter("@Memo", model.Memo),
					new SqlParameter("@Creator", model.Creator),
					new SqlParameter("@CreateTime", model.CreateTime),
					new SqlParameter("@IsModify", model.IsModify)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  保存一条数据
		/// </summary>
		public bool Update(Sys_Config model)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("update [Sys_Config] set ");
			strSql.Append("[ConfigValue]=@ConfigValue,[ConfigType]=@ConfigType,[Status]=@Status,[Memo]=@Memo,[LastModifier]=@LastModifier,[LastModifyTime]=@LastModifyTime,[IsModify]=@IsModify");
			strSql.Append(" where [ConfigKey]=@ConfigKey");

			SqlParameter[] parameters = {
				  
					new SqlParameter("@ConfigKey", model.ConfigKey),
					new SqlParameter("@ConfigValue", model.ConfigValue),
					new SqlParameter("@ConfigType", model.ConfigType),
					new SqlParameter("@Status", model.Status),
					new SqlParameter("@Memo", model.Memo),
					new SqlParameter("@LastModifier", model.LastModifier),
					new SqlParameter("@LastModifyTime", model.LastModifyTime),
					new SqlParameter("@IsModify", model.IsModify)
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		///</summary>
		public bool Delete(String ConfigKey)
		{
			StringBuilder strSql = new StringBuilder();
			strSql.Append("delete from [Sys_Config] ");
			strSql.Append(" where [ConfigKey]=@ConfigKey");

			SqlParameter[] parameters = {
			
				new SqlParameter("@ConfigKey", ConfigKey)             
			  
			};

			int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Sys_Config GetOneModel(String configKey)
		{
			string sql = "select top 1 ConfigKey, ConfigValue, ConfigType, Status, Memo, Creator, CreateTime, LastModifier, LastModifyTime, IsModify from Sys_Config where ConfigKey = @ConfigKey and [Status] = 1";

			SqlParameter[] parameters = {
					new SqlParameter("@ConfigKey", configKey)
										};

			DataSet ds = DbHelperSQL.Query(sql, parameters);
			if (ds.Tables[0].Rows.Count > 0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Sys_Config GetModel(String configKey)
        {
            string sql = "select top 1 ConfigKey, ConfigValue, ConfigType, Status, Memo, Creator, CreateTime, LastModifier, LastModifyTime, IsModify from Sys_Config where ConfigKey = @ConfigKey ";

            SqlParameter[] parameters = {
					new SqlParameter("@ConfigKey", configKey)
										};

            DataSet ds = DbHelperSQL.Query(sql, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Sys_Config DataRowToModel(DataRow row)
		{
			if (row == null) return null;

			Sys_Config model = new Sys_Config();

			if (row["ConfigKey"] != DBNull.Value)
			{
				model.ConfigKey = (String)row["ConfigKey"];
			}

			if (row["ConfigValue"] != DBNull.Value)
			{
				model.ConfigValue = (String)row["ConfigValue"];
			}

			if (row["ConfigType"] != DBNull.Value)
			{
				model.ConfigType = (String)row["ConfigType"];
			}

			if (row["Status"] != DBNull.Value)
			{
				model.Status = (Int32)row["Status"];
			}

			if (row["Memo"] != DBNull.Value)
			{
				model.Memo = (String)row["Memo"];
			}

			if (row["Creator"] != DBNull.Value)
			{
				model.Creator = (String)row["Creator"];
			}

			if (row["CreateTime"] != DBNull.Value)
			{
				model.CreateTime = (DateTime)row["CreateTime"];
			}

			if (row["LastModifier"] != DBNull.Value)
			{
				model.LastModifier = (String)row["LastModifier"];
			}

			if (row["LastModifyTime"] != DBNull.Value)
			{
				model.LastModifyTime = (DateTime)row["LastModifyTime"];
			}

			if (row["IsModify"] != DBNull.Value)
			{
				model.IsModify = (Boolean)row["IsModify"];
			}    
			return model;
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string configKey)
		{
			string sql = "select count(1) from Sys_Config where ConfigKey = @ConfigKey";

			SqlParameter[] parameters = {
					new SqlParameter("@ConfigKey", configKey)			
										};

			return DbHelperSQL.Exists(sql, parameters);
		}


		/// <summary>
		/// 获得数据列表分页
		/// </summary>
		public DataTable GetPager(string configKey, int pageindex, int pagesize, out int total)
		{
			string sql = @"select ConfigKey, ConfigValue, ConfigType, Status, Memo, Creator, CreateTime, LastModifier, LastModifyTime, IsModify
							from Sys_Config";

			List<SqlParameter> parameters = new List<SqlParameter>();

			if (!string.IsNullOrEmpty(configKey))
			{
				sql += " where ConfigKey like @ConfigKey";
				parameters.Add(new SqlParameter("@ConfigKey", "%" + configKey + "%"));
			}
            return DbHelperSQL.GetPager(pageindex, pagesize, sql, "order by ConfigType,ConfigKey desc ", out total, parameters.ToArray());
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet Gets(string sqlWhere)
		{
			StringBuilder strSql = new StringBuilder();

			strSql.Append("select * from [Sys_Config] ");
			if (sqlWhere.Trim() != "")
			{
				strSql.Append(" where " + sqlWhere);
			}

			return DbHelperSQL.Query(strSql.ToString());
		}
		#endregion  成员方法

	}
}


  